package com.example.com.common.utils.bean;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class CryptAES {
    static String keyStr = "sfjIud2thf56p2kK";
    static String iv = "weifsaOAoS804ty5";
    //private static final String AESTYPE ="AES/ECB/PKCS5Padding";
    private static final String AESTYPE ="AES/CBC/PKCS5Padding";
    /**将二进制转换成16进制
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**将16进制转换为二进制
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length()/2];
        for (int i = 0;i< hexStr.length()/2; i++) {
            int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
            int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
    public static String AES_Encrypt(String plainText) {
        byte[] encrypt = null;
        try{
            Key key = generateKey(keyStr);
            Cipher cipher = Cipher.getInstance(AESTYPE);
            //cipher.init(Cipher.ENCRYPT_MODE, key);
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(getIV(iv)));//使用加密模式初始化 密钥
            encrypt = cipher.doFinal(plainText.getBytes());
        }catch(Exception e){
            e.printStackTrace();
        }
        return parseByte2HexStr(encrypt);
    }

    public static String AES_Decrypt(String encryptData) {
        byte[] decrypt = null;
        try{
            Key key = generateKey(keyStr);
            Cipher cipher = Cipher.getInstance(AESTYPE);
            //cipher.init(Cipher.DECRYPT_MODE, key);
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(getIV(iv)));//使用解密模式初始化 密钥
            //decrypt = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptData));
            decrypt = cipher.doFinal(parseHexStr2Byte(encryptData));

        }catch(Exception e){
            e.printStackTrace();
        }
        return new String(decrypt).trim();
    }

    private static Key generateKey(String key)throws Exception{
        try{
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
            
            return keySpec;
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }

    }

    private static byte[] getIV(String originalIV) {
        String iv = originalIV; //IV length: must be 16 bytes long
        return iv.getBytes();
    }

    public static void main(String[] args) {

        String keyStr = "sfjIud2thf56p2kK";
        String iv = "weifsaOAoS804ty5";
       
        String plainText = "49";
        
       String encText = AES_Encrypt(plainText);
       System.out.println("密文："+encText);
        String decString = AES_Decrypt("3334518C69DFA9524A6AE9A6934EDD58");

        //System.out.println(encText);
        System.out.println("原文:"+decString);

    }
}